package com.xiaoshuidi.cloud.module.rooms.repository;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xiaoshuidi.cloud.framework.common.exception.ServiceException;
import com.xiaoshuidi.cloud.framework.common.exception.enums.GlobalErrorCodeConstants;
import com.xiaoshuidi.cloud.module.rooms.controller.admin.roomequipmentcategory.vo.EquipmentCategoryPageReqVO;
import com.xiaoshuidi.cloud.module.rooms.controller.admin.roomequipmentcategory.vo.EquipmentCategoryPageResp;
import com.xiaoshuidi.cloud.module.rooms.mapper.EquipmentCategoryMapper;
import com.xiaoshuidi.cloud.module.rooms.pojo.EquipmentCategory;
import com.xiaoshuidi.cloud.module.rooms.repository.irepository.IEquipmentCategoryRepository;
import lombok.RequiredArgsConstructor;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.Map;


/**
 * @Author Feng
 * @date 2023/4/27
 */
@Repository
@RequiredArgsConstructor
public class EquipmentCategoryRepository extends ServiceImpl<EquipmentCategoryMapper, EquipmentCategory> implements IEquipmentCategoryRepository {

    private final EquipmentCategoryMapper mapper;

    @Override
    public IPage<EquipmentCategoryPageResp> pageQuery(EquipmentCategoryPageReqVO reqVO) {
        Page<EquipmentCategoryPageResp> page = new Page<>(reqVO.getPageNo(), reqVO.getPageSize());
        IPage<EquipmentCategoryPageResp> pageQuery = mapper.pageQuery(page, reqVO);
        return pageQuery;
    }

    @Override
    public List<EquipmentCategory> getTreeList(Long parentId) {
        LambdaQueryWrapper<EquipmentCategory> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(parentId != null && !parentId.equals(0L), EquipmentCategory::getPcategoryId, parentId)
                .and(parentId == null || parentId.equals(0L), s -> s.isNull(EquipmentCategory::getPcategoryId)
                        .or().eq(EquipmentCategory::getPcategoryId, 0));
        return list(wrapper);
    }

    @Override
    public List<Map<String, Object>> getZcList() {
        LambdaQueryWrapper<EquipmentCategory> wrapper = Wrappers.lambdaQuery();

        wrapper
                .select(EquipmentCategory::getId, EquipmentCategory::getName)
                .orderByAsc(EquipmentCategory::getName);

        return mapper.selectMaps(wrapper);
    }

    @Override
    public Boolean validateName(String name) {
        List<EquipmentCategory> list = mapper.selectList(new LambdaQueryWrapper<EquipmentCategory>().eq(EquipmentCategory::getName, name));
        if (ObjectUtils.isNotEmpty(list)) {
            throw new ServiceException(GlobalErrorCodeConstants.INTERNAL_SERVER_ERROR.getCode(), "类别名称已存在");
        }
        return true;
    }

    @Override
    public Long countByParentId(Long parentId) {
        LambdaQueryWrapper<EquipmentCategory> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(EquipmentCategory::getPcategoryId, parentId);
        return count(wrapper);
    }

}
